//
//  BackgroundSettingViewController.m
//  Finder
//
//  Created by cdm on 11-4-1.
//  Copyright 2011年 __MyCompanyName__. All rights reserved.
//

#import "BackgroundSettingViewController.h"
#import "BackgroundItemViewController.h"

@implementation BackgroundSettingViewController

@synthesize imageScrollView;
@synthesize imageScrollViewPageControl;
@synthesize imageScrollViewStartOffset;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}
-(void)weihuida:(NSString *)strT
{
    UIView *ceshi=[[UIView alloc]init];
    [ceshi setTag:951];
    UIImageView *whdbg=[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"weihuida.png"]];
    [whdbg setCenter:CGPointMake(160, 400)];
    UILabel *txt=[[UILabel alloc]initWithFrame:CGRectMake(20, 8, 300, 30) ];
    [txt setText:strT];
    [txt setTextColor:[UIColor whiteColor]];
    [txt setBackgroundColor:[UIColor clearColor]];
    [txt setFont:[UIFont boldSystemFontOfSize:15]];
    [txt setTextAlignment:UITextAlignmentCenter];
    [whdbg addSubview:txt];
    [ceshi setAlpha:0.0f];
    [ceshi addSubview:whdbg];
    [[UIApplication sharedApplication].keyWindow addSubview:ceshi];
	
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:0.3f];
    [ceshi setAlpha:1.0f];
    [UIView commitAnimations];
	
	[whdbg release];
	[txt release];
	
	[ceshi release];
	
    [NSTimer scheduledTimerWithTimeInterval:10.0f target:self selector:@selector(xiaoshi) userInfo:nil repeats:NO];
    
}
-(void)xiaoshi
{
    UIView *ceshi=(UIView*)[[UIApplication sharedApplication].keyWindow viewWithTag:951];
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:0.2f];
    [ceshi setAlpha:0.0f];
    [UIView commitAnimations];
    [ceshi removeFromSuperview];
}

- (void)dealloc
{
    [self.imageScrollView release];
    [self.imageScrollViewPageControl release];
    [super dealloc];
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView
{
}
*/


// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{
    [super viewDidLoad];
    
    //NSString *alertString =[[NSString alloc] initWithString:@"左右滑动查看，单击选择背景"]; 
    //[self weihuida:alertString];
    
    
    for (int i = 0; i < 6; i++) {
        NSString *imageName = [NSString stringWithFormat:@"app_bg_%d.jpg", i+1];
        BackgroundItemViewController *itemViewController = [[BackgroundItemViewController alloc] initWithNibName:@"BackgroundItemView" bundle:nil];
        [itemViewController loadView];
        [itemViewController setImage:imageName];
        
        CGRect itemFrame = itemViewController.view.frame;
        itemFrame.origin.x = i*itemFrame.size.width;
        itemViewController.view.frame = itemFrame;
        [self.imageScrollView addSubview:itemViewController.view];
        
        CGSize contentSize = self.imageScrollView.contentSize;
        contentSize.width += itemViewController.view.frame.size.width;
        self.imageScrollView.contentSize = contentSize;
        
        //[itemViewController release];
        
         self.imageScrollViewPageControl.numberOfPages++;
    }
    
   
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

#pragma mark - Scroll view delegate

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    imageScrollViewStartOffset = scrollView.contentOffset.x;
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    if (scrollView.contentOffset.x > imageScrollViewStartOffset) {
        self.imageScrollViewPageControl.currentPage++;
    } else if (scrollView.contentOffset.x < imageScrollViewStartOffset) {
        self.imageScrollViewPageControl.currentPage--;
    }
}


@end
